//
//  nevarByxqVC.m
//  lyxluyou
//
//  Created by mmyy on 15/10/18.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "nevarByxqVC.h"
#import "nevarByxqItems.h"
#import "StarView.h"
#import "mapView.h"
#import "moreImage.h"
#import "xiangqinCell.h"
@interface nevarByxqVC ()

@end

@implementation nevarByxqVC
{
    UIScrollView *_scrollView;
    NSMutableArray *_dataSource;
    nevarByxqItems *_item;
    UIImageView *_bgImage;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //创建navcationg
    [self createNavcation];
    
    [self createloadDataImage];
    //加载数据
    [self loadData];
    //创建SCRollView
    
    
    
    // Do any additional setup after loading the view.
}
#pragma mark -- navationg
-(void)createNavcation
{
    
}

#pragma mark -- loaddata
-(void)loadData
{
    _bgImage.hidden = NO;
    _dataSource = [NSMutableArray arrayWithCapacity:0];
    [Funcation getWithURL:[NSString stringWithFormat:nearbyxqUrl, self.vid] success:^(id responseObject) {
        _bgImage.hidden = YES;
        _item = [nevarByxqItems nevarByxqItemsWithDict:responseObject];
        [self createScrollView];
    } andFail:^(id error) {
        NSLog(@"Fail -- ");
        
    }];

}
#pragma mark -- 创建ScrollView
-(void)createScrollView
{
    _scrollView = [UIScrollView new];
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.top).offset(64);
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.bottom.equalTo(self.view.bottom).offset(0);
    }];
    UIImageView *headImage = [UIImageView new];
    headImage.userInteractionEnabled = YES;
    [headImage setImageWithURL:[NSURL URLWithString:_item.photo] placeholderImage:[UIImage imageNamed:@"define"]];
    [_scrollView addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView.top).offset(5);
        make.left.equalTo(_scrollView.left).offset(5);
        make.right.equalTo(_scrollView.right).offset(-5);
        make.height.equalTo(@200);
        CGFloat width = self.view.frame.size.width - 10;
        make.width.equalTo(width);
    }];
    UIButton *gd = [UIButton new];
    [gd setTitle:[NSString stringWithFormat:@"更多相片"] forState:UIControlStateNormal];
    [gd setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    gd.titleLabel.font = [UIFont systemFontOfSize:10];
    gd.titleLabel.adjustsFontSizeToFitWidth = YES;
    gd.titleLabel.textColor = [UIColor greenColor];
    [headImage addSubview:gd];
    [gd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headImage.right).offset(-10);
        make.bottom.equalTo(headImage.bottom).offset(-5);
        make.width.equalTo(@80);
        make.height.equalTo(10);
    }];
    [gd addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *jianjie = [UIView new];
    jianjie.backgroundColor = [UIColor clearColor];
    jianjie.alpha = 0.2;
    [_scrollView addSubview:jianjie];
    [jianjie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scrollView.left).offset(5);
        make.top.equalTo(headImage.bottom).offset(5);
        make.height.equalTo(@150);
        CGFloat width = self.view.frame.size.width - 10;
        make.width.equalTo(width);
    }];
    UILabel *title = [UILabel new];
    title.text = _item.name;
    title.font = [UIFont boldSystemFontOfSize:25];
    title.textColor = [UIColor blackColor];
    title.backgroundColor = [UIColor clearColor];
    title.textAlignment = NSTextAlignmentCenter;
    [jianjie addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jianjie.top).offset(10);
        make.left.equalTo(jianjie.left).offset(0);
        CGFloat width = self.view.frame.size.width - 20;
        make.width.equalTo(width);
        make.height.equalTo(@50);
    }];
    StarView *star = [[StarView alloc] init];
    [jianjie addSubview:star];
    [star setStarValue:[_item.rating floatValue] / 5.0];
    [star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(title.bottom).offset(5);
        make.left.equalTo(@100);
        make.width.equalTo(@65);
        make.height.equalTo(@23);
    }];
    
    UILabel *dianping = [UILabel new];
    dianping.adjustsFontSizeToFitWidth = YES;
    dianping.textColor = [UIColor blackColor];
    dianping.font = [UIFont systemFontOfSize:15];
    dianping.backgroundColor = [UIColor clearColor];
    dianping.text = [NSString stringWithFormat:@"%@人参与点评", _item.rating_users];
    [jianjie addSubview:dianping];
    [dianping mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(title.bottom).offset(0);
        make.left.equalTo(star.right).offset(5);
        make.right.equalTo(jianjie.right).offset(-10);
        make.height.equalTo(@23);
    }];
    UILabel *recommended_reason = [UILabel new];
    recommended_reason.textColor = [UIColor blackColor];
    if (![_item.recommended_reason isKindOfClass:[NSNull class]]) {
         recommended_reason.text = _item.recommended_reason;
    }
   
    recommended_reason.font = [UIFont systemFontOfSize:15];
    recommended_reason.lineBreakMode = NSLineBreakByWordWrapping;
    recommended_reason.numberOfLines = 2;
    recommended_reason.adjustsFontSizeToFitWidth = YES;
    recommended_reason.textAlignment = NSTextAlignmentCenter;
    [jianjie addSubview:recommended_reason];
    [recommended_reason mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dianping.bottom).offset(5);
        make.left.equalTo(jianjie.left).offset(10);
        make.right.equalTo(jianjie.right).offset(-10);
        make.bottom.equalTo(jianjie.bottom).offset(-5);
    }];
    UILabel *xinxi = [UILabel new];
    xinxi.text = @"------------- <基本信息> ---------------";
    xinxi.font = [UIFont boldSystemFontOfSize:20];
    xinxi.adjustsFontSizeToFitWidth = YES;
    xinxi.textColor = [UIColor blackColor];
    xinxi.textAlignment = NSTextAlignmentCenter;
    xinxi.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:xinxi];
    [xinxi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jianjie.bottom).offset(5);
        make.left.equalTo(_scrollView.left).offset(0);
        make.right.equalTo(_scrollView.right).offset(0);
        make.height.equalTo(@30);
    }];
    UILabel *gaik = [UILabel new];
    gaik.text = @"概况:";
    gaik.font = [UIFont boldSystemFontOfSize:20];
    gaik.textColor = [UIColor blackColor];
    gaik.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:gaik];
    [gaik mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(xinxi.bottom).offset(5);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(@50);
        make.height.equalTo(@30);
    }];
    UILabel *gaikuanxi = [UILabel new];
    gaikuanxi.text = [NSString stringWithFormat:@"        %@",_item.vdescription];
    if ([_item.vdescription length] == 0) {
        gaikuanxi.text = @"暂无简介";
    }
    gaikuanxi.adjustsFontSizeToFitWidth = YES;
    gaikuanxi.font = [UIFont systemFontOfSize:15];
    gaikuanxi.textColor = [UIColor blackColor];
    gaikuanxi.lineBreakMode = NSLineBreakByWordWrapping;
    gaikuanxi.numberOfLines = 0;
    gaikuanxi.layer.borderColor = [UIColor blackColor].CGColor;
    gaikuanxi.layer.borderWidth = 1;
    [_scrollView addSubview:gaikuanxi];
    [gaikuanxi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(gaik.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        CGFloat width = self.view.frame.size.width - 20;
        make.width.equalTo(width);
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        
        NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],
                                  NSFontAttributeName,
                                  paragraphStyle,
                                  NSParagraphStyleAttributeName,
                                  nil];
        CGRect expectedRect = [gaikuanxi.text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
        CGSize expectedSize = expectedRect.size;
        
    
        make.height.equalTo(expectedSize.height);
    }];
    
    UILabel *dizhi = [UILabel new];
    dizhi.text = @"地址:";
    dizhi.font = [UIFont boldSystemFontOfSize:20];
    dizhi.textColor = [UIColor blackColor];
    dizhi.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:dizhi];
    [dizhi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(gaikuanxi.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(@50);
        make.height.equalTo(@30);
    }];
    
    UILabel *xiangxidizhi = [UILabel new];
    xiangxidizhi.text = _item.address;
    xiangxidizhi.font = [UIFont systemFontOfSize:15];
    xiangxidizhi.textColor = [UIColor blackColor];
    xiangxidizhi.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:xiangxidizhi];
    [xiangxidizhi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dizhi.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(@200);
        make.height.equalTo(@30);
    }];
    UIButton *quzheer = [UIButton new];
    [quzheer setTitle:@"去这儿" forState:UIControlStateNormal];
    quzheer.backgroundColor = [UIColor orangeColor];
    quzheer.layer.cornerRadius = 10;
    quzheer.layer.masksToBounds = YES;
    [quzheer setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_scrollView addSubview:quzheer];
    [quzheer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dizhi.bottom).offset(0);
        make.right.equalTo(_scrollView.right).offset(-10);
        make.width.equalTo(@80);
        make.height.equalTo(@25);
    }];
    
    UILabel *daoda = [UILabel new];
    daoda.text = @"到达方式:";
    daoda.font = [UIFont boldSystemFontOfSize:20];
    daoda.textColor = [UIColor blackColor];
    daoda.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:daoda];
    [daoda mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(xiangxidizhi.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
       
        
    }];
    UILabel *daodaofs = [UILabel new];
    if (![_item.arrival_typel isKindOfClass:[NSNull class]]) {
        daodaofs.text = _item.arrival_typel;
    }
    
    daodaofs.font = [UIFont systemFontOfSize:15];
    daodaofs.textColor = [UIColor blackColor];
    daodaofs.backgroundColor = [UIColor clearColor];
    daodaofs.lineBreakMode = NSLineBreakByWordWrapping;
    daodaofs.numberOfLines = 0;
    [_scrollView addSubview:daodaofs];
    [daodaofs mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(daoda.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        CGFloat width = self.view.frame.size.width - 20;
        make.width.equalTo(width);
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        
        NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],
                                  NSFontAttributeName,
                                  paragraphStyle,
                                  NSParagraphStyleAttributeName,
                                  nil];
        CGRect expectedRect = [daodaofs.text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
        CGSize expectedSize = expectedRect.size;
        
        make.height.equalTo(expectedSize.height);
        
    }];
    
    UILabel *opentime = [UILabel new];
    opentime.text = @"开放时间:";
    opentime.font = [UIFont boldSystemFontOfSize:20];
    opentime.textColor = [UIColor blackColor];
    opentime.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:opentime];
    [opentime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(daodaofs.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
    }];
    
    UILabel *time = [UILabel new];
    if (![_item.opening_time isKindOfClass:[NSNull class]]) {
        time.text = _item.opening_time;
    }
    
    time.font = [UIFont systemFontOfSize:15];
    time.textColor = [UIColor blackColor];
    time.backgroundColor = [UIColor clearColor];
    time.adjustsFontSizeToFitWidth = YES;
    [_scrollView addSubview:time];
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(opentime.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        CGFloat width = self.view.frame.size.width - 20;
        make.width.equalTo(width);
        make.height.equalTo(30);
        
        
    }];
    
    UILabel *telphone = [UILabel new];
    telphone.text = @"联系方式:";
    telphone.font = [UIFont boldSystemFontOfSize:20];
    telphone.textColor = [UIColor blackColor];
    telphone.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:telphone];
    [telphone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(time.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
        
        
    }];
    UILabel *tel = [UILabel new];
    tel.text = _item.tel;
    tel.font = [UIFont systemFontOfSize:15];
    tel.textColor = [UIColor blackColor];
    tel.backgroundColor = [UIColor clearColor];
    tel.adjustsFontSizeToFitWidth = YES;
    [_scrollView addSubview:tel];
    [tel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(telphone.bottom).offset(0);
        make.left.equalTo(_scrollView.left).offset(10);
        make.width.equalTo(200);
        make.height.equalTo(30);
        
        
    }];
    
    UIButton *carMe = [UIButton new];
    [carMe setTitle:@"联系我们" forState:UIControlStateNormal];
    carMe.backgroundColor = [UIColor greenColor];
    carMe.layer.cornerRadius = 10;
    carMe.layer.masksToBounds = YES;
    [carMe setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_scrollView addSubview:carMe];
    [carMe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(telphone.bottom).offset(0);
        make.right.equalTo(_scrollView.right).offset(-10);
        make.width.equalTo(@80);
        make.height.equalTo(@25);
        make.bottom.equalTo(_scrollView.bottom).offset(-10);
    }];
    [quzheer addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [carMe addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    

}
-(void)createloadDataImage
{
    _bgImage = [[UIImageView alloc] init];
    _bgImage.backgroundColor = [UIColor blackColor];
    _bgImage.alpha = 0.6;
    [self.view addSubview:_bgImage];
    CGFloat width = (self.view.frame.size.width - 100) / 2.0;
    CGFloat height = (self.view.frame.size.height - 100) / 2.0;
    [_bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@100);
        make.height.equalTo(@100);
        make.top.equalTo(self.view.top).offset(height);
        make.left.equalTo(self.view.left).offset(width);
        
    }];
    UIActivityIndicatorView *activ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activ.frame = CGRectMake(10, 0,  80, 80);
    activ.backgroundColor = [UIColor clearColor];
    activ.color = [UIColor whiteColor];
    activ.transform = CGAffineTransformMakeScale(2, 2);
    [_bgImage addSubview:activ];
    [activ startAnimating];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 80, 20)];
    label.textColor = [UIColor whiteColor];
    label.text = @"加载中...";
    label.textAlignment = NSTextAlignmentCenter;
    label.adjustsFontSizeToFitWidth = YES;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:15];
    [_bgImage addSubview:label];
    _bgImage.hidden = YES;
}
-(void)buttonClick:(UIButton *)sender
{
    if ([sender.currentTitle isEqualToString:@"去这儿"]) {
        mapView *view = [mapView new];
        CLLocationCoordinate2D coor = CLLocationCoordinate2DMake([_item.lat floatValue], [_item.lng floatValue]);
        view.coor = coor;
        view.title = _item.name;
        view.subTitle = _item.arrival_typel;
        [self.navigationController pushViewController:view animated:YES];
    }else if([sender.currentTitle isEqualToString:@"更多相片"]){
        moreImage *more = [moreImage new];
        more.vid = [_item.vid stringValue];
        [self.navigationController pushViewController:more animated:YES];
    }else {
        if ([_item.tel length] != 0) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", _item.tel]]];
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"电话号码为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
